<template>
  <el-dialog :title="!dataForm.attrGroupId ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" @closed="dialogClose">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-form-item label="组名" prop="attrGroupName">
        <el-input v-model="dataForm.attrGroupName" placeholder="组名"></el-input>
      </el-form-item>
      <el-form-item label="所属分类" prop="catelogId">
        <!-- <el-input v-model="dataForm.catelogId" placeholder="所属分类"></el-input> -->
        <el-cascader v-model="dataForm.catelogIds" :options="categorys" @change="handleChange" :props="props" 
            style="width: 300px;" filterable placeholder="试试搜索: 手机"></el-cascader>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="descript">
        <el-input v-model="dataForm.descript" placeholder="描述"></el-input>
      </el-form-item>
      <el-form-item label="组图标" prop="icon">
        <el-input v-model="dataForm.icon" placeholder="组图标"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        props:{
          label:"name",
          value:"catId"
        },
        visible: false,
        dataForm: {
          attrGroupId: 0,
          attrGroupName: '',
          sort: '',
          descript: '',
          icon: '',
          catelogId: '',
          catelogIds: []  //分类选择框需要绑定数组, 表单提交使用catelogId字段,在handleChange()方法中处理
        }, 
        categorys:[],
        dataRule: {
          attrGroupName: [
            { required: true, message: '组名不能为空', trigger: 'blur' }
          ],
          // sort: [
          //   { required: true, message: '排序不能为空', trigger: 'blur' }
          // ],
          // descript: [
          //   { required: true, message: '描述不能为空', trigger: 'blur' }
          // ],
          // icon: [
          //   { required: true, message: '组图标不能为空', trigger: 'blur' }
          // ],
          catelogId: [
            { required: true, message: '所属分类id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      //修改关闭后清除 所属分类-多级选项内容
      dialogClose(){
        this.dataForm.catelogIds = [];
      },
      // 查询分类内容
      getCategorys(){

        this.$http({
          url: this.$http.adornUrl('/product/category/list/tree'),
          method: 'get',
        }).then(({ data }) => {
          //{data} 把数据中的data解构出来
          this.categorys = data.data;
        })

      },
      init (id) {
        this.dataForm.attrGroupId = id || 0;
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields() //恢复默认
          if (this.dataForm.attrGroupId) {
            this.$http({
              url: this.$http.adornUrl(`/product/attrgroup/info/${this.dataForm.attrGroupId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.attrGroupName = data.data.attrGroupName
                this.dataForm.sort = data.data.sort
                this.dataForm.descript = data.data.descript
                this.dataForm.icon = data.data.icon
                this.dataForm.catelogId = data.data.catelogId
                //查出catelogId的完整路径,回显树 catelogPath
                this.dataForm.catelogIds = data.data.catelogPath;
              }
            })
          }
        })
      },
      //监听所属分类的变化  attr-add-or-update中使用 监视器 来实现
      handleChange(valueArr) {
        //pop() 方法用于从数组中删除并返回最后一个元素。
        let catelogId = valueArr.pop();
        this.dataForm.catelogId = catelogId;
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/product/attrgroup/${!this.dataForm.attrGroupId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'attrGroupId': this.dataForm.attrGroupId || undefined,
                'attrGroupName': this.dataForm.attrGroupName,
                'sort': this.dataForm.sort,
                'descript': this.dataForm.descript,
                'icon': this.dataForm.icon,
                'catelogId': this.dataForm.catelogId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList'); // 保存后向父组件发送一个事件,父组件会见
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    },
    //生命周期函数 在实例创建完成后被立即同步调用。 
    created() {
      this.getCategorys();
    }
  }
</script>
